import Taro from '@tarojs/taro'

/**
 * 成功提示
 * @param {string} title 提示内容
 * @param {number} duration 显示时长(ms)，默认1500ms
 */
export const success = (title, duration = 1500) => {
  Taro.showToast({
    title,
    icon: 'success',
    duration,
    mask: true
  })
}

/**
 * 错误提示
 * @param {string} title 提示内容
 * @param {number} duration 显示时长(ms)，默认1500ms
 */
export const error = (title, duration = 1500) => {
  Taro.showToast({
    title,
    icon: 'error',
    duration,
    mask: true
  })
}

/**
 * 警告提示
 * @param {string} title 提示内容
 * @param {number} duration 显示时长(ms)，默认1500ms
 */
export const warning = (title, duration = 1500) => {
  Taro.showToast({
    title,
    icon: 'none',
    duration,
    mask: true
  })
}

/**
 * 加载提示
 * @param {string} title 提示内容
 * @param {boolean} mask 是否显示遮罩，默认true
 */
export const loading = (title = '加载中...', mask = true) => {
  Taro.showLoading({
    title,
    mask
  })
}

/**
 * 隐藏加载提示
 */
export const hideLoading = () => {
  Taro.hideLoading()
}

/**
 * 普通提示
 * @param {string} title 提示内容
 * @param {Object} options 配置项
 */
export const info = (title, options = {}) => {
  const defaultOptions = {
    icon: 'none',
    duration: 1500,
    mask: true
  }

  Taro.showToast({
    title,
    ...defaultOptions,
    ...options
  })
}

/**
 * showModal
 */

/**
 * 显示模态对话框
 * @param {Object} options 配置项
 * @returns {Promise} 返回用户操作结果
 */
export const modal = (options = {}) => {
  const defaultOptions = {
    title: '提示',
    content: '',
    showCancel: true,
    cancelText: '取消',
    cancelColor: '#000000',
    confirmText: '确定',
    confirmColor: '#576B95'
  }

  return new Promise((resolve, reject) => {
    Taro.showModal({
      ...defaultOptions,
      ...options,
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}


export default {
  success,
  error,
  warning,
  loading,
  hideLoading,
  info,
  modal
}
